package com.cat.dataStructure;

import java.nio.channels.InterruptedByTimeoutException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/identify-the-largest-outlier-in-an-array/description/
 * @create 2025/8/14 09:04
 * @since JDK17
 */

public class Solution03 {
    HashMap<Integer, Integer> map = new HashMap<>();
    public int getLargestOutlier(int[] nums) {
        int ans = Integer.MIN_VALUE, n = nums.length, sum = 0;
        map.clear();
        for (int i = 0; i < n; i++) {
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
            sum += nums[i];
        }
        for (int i = 0, t; i < n; i++) {
            t = sum - nums[i];
            if (t % 2 == 0 && map.containsKey(t / 2)) {    // 可以成为一个异常值
                if (t / 2 == nums[i] && map.get(nums[i]) <= 1) {
                    continue;
                }
                ans = Math.max(ans, nums[i]);
            }
        }
        return ans;
    }
}
